import { getCookiesInClient, getCookiesInServer } from '~~/utils/getCookie';

export default defineNuxtRouteMiddleware(async (to) => {
	const isServer = process.server;
	const isClient = process.client;

	let token = '';

	if (isServer) {
		const cookies = getCookiesInServer();
		token = cookies['token'];
	}
	if (isClient) {
		const cookies = getCookiesInClient();
		token = cookies.get('token');
	}
	if (!['signup', 'login'].includes(to.name as string) && !token) {
		return navigateTo('/login');
	}
});
